package com.ijiuchuang.jc_localization.tools.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.http.Part;

/**
 * Created by Veev on 2016/7/2
 *
 * @link 384506557@qq.com
 */
public class DateUtil {

    /**
     * 处理时间 例：2016-04-18 15:03:50，返回显示字符串
     *
     * @return 5分钟以内返回刚刚，5分钟以外且为当天显示时：分，不是当天显示月日
     */
    public static String dealTime(String strTime) {
        String[] originalTime = splitTime(strTime);
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] currentTime = splitTime(simpleDateFormat.format(now));
        
        int org_year = Integer.parseInt(originalTime[0]);
        int org_month = Integer.parseInt(originalTime[1]);
        int org_day = Integer.parseInt(originalTime[2]);
        int org_hour = Integer.parseInt(originalTime[3]);
        int org_minute = Integer.parseInt(originalTime[4]);
        //int org_second = Integer.parseInt(originalTime[5]);

        int cur_year = Integer.parseInt(currentTime[0]);
        int cur_month = Integer.parseInt(currentTime[1]);
        int cur_day = Integer.parseInt(currentTime[2]);
        int cur_hour = Integer.parseInt(currentTime[3]);
        int cur_minute = Integer.parseInt(currentTime[4]);
        int cur_second = Integer.parseInt(currentTime[5]);
        
        if (org_year == cur_year) {
            if (org_month == cur_month) {
                if (org_day == cur_day) {
                    if (org_hour == cur_hour) {
                        if (org_minute <= cur_minute - 5){
                            return "刚刚";
                        } else {
                            return cur_minute - org_minute + "分钟前";
                        }
                    } else if (org_hour <= 12) {
                        return "上午" + org_hour + ":" + org_minute;
                    } else {
                        return "下午" + org_hour + ":" + org_minute;
                    }
                } else if (org_day == cur_day - 1) {
                    return "昨天" + org_hour + "时";
                } else {
                    return "";
                }
            } else {
                return org_month + "月" + org_day + "日";
            }
        } else if (org_year == cur_year - 1) {
            return "去年 " + org_month + "月" + org_day + "日";
        } else {
            return org_year + "年 " + org_month + "月";
        }
    }

    /**
     * 拆分时间，将形如2016-04-18 15:03:50 拆分成数组返回
     *
     * @return 数组，长度为6，分别是年、月、日、时、分、秒
     */
    private static String[] splitTime(String strTime) {
        String str = strTime.replace("-", " ").replace(":", " ");
        return str.split(" ");
    }
}
